/**
 * 订单信息管理初始化
 */
var UserInfo = {
    id: "UserInfoTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
UserInfo.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: '订单编号', field: 'orderNo', visible: false, align: 'center', valign: 'middle'},
        {title: '客户姓名', field: 'userName', visible: true, align: 'center', valign: 'middle'},
        {title: '客户手机号', field: 'userPhone', visible: true, align: 'center', valign: 'middle'},
        {title: '地址', field: 'address', visible: true, align: 'center', valign: 'middle'},
        {title: '录入时间', field: 'createTime', visible: true, align: 'center', valign: 'middle',formatter:UserInfo.dateFormat},
        {title: '备注信息', field: 'remarks', align: 'center', valign: 'middle', sortable: true,formatter:UserInfo.return2Br}
    ];
};

UserInfo.return2Br=function(value){
    console.log(value);
    var arrEntities={'lt':'<','gt':'>','nbsp':' ','amp':'&','quot':'"'};
    var result='';
    if(value!=undefined){
        result= value.replace(/<[^<>]+?>/g,'');
        //replace(/&(lt|gt|nbsp|amp|quot);/ig,function(all,t){return arrEntities[t];});
    }else {
        result= value;
    }
    console.log(result);
    return result;
}


UserInfo.dateFormat=function(value){
    if(value!=undefined){
        var date =  new Date(value);
        var y = 1900+date.getYear();
        var m = "0"+(date.getMonth()+1);
        var d = "0"+date.getDate();
        return y+"-"+m.substring(m.length-2,m.length)+"-"+d.substring(d.length-2,d.length);
    }else {
        return "";
    }
}


/**
 * 检查是否选中
 */
UserInfo.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        UserInfo.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加订单信息
 */
UserInfo.openAddUserInfo = function () {
    var index = layer.open({
        type: 2,
        title: '添加订单信息',
        area: ['1000px', '550px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/userInfo/userInfo_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看订单信息详情
 */
UserInfo.openUserInfoDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '客户信息详情',
            area: ['1000px', '550px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/userInfo/userInfo_update/' + UserInfo.seItem.id
        });
        this.layerIndex = index;
    }
};



/**
 * 打开查看订单信息详情
 */
UserInfo.openOrderDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '客户下单',
            area: ['1100px', '600px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/userInfo/order_update/' + UserInfo.seItem.id
        });
        this.layerIndex = index;
    }
};


/**
 * 删除客户信息信息
 */
UserInfo.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/userInfo/delete", function (data) {
            Feng.success("删除成功!");
            UserInfo.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("orderId",this.seItem.id);
        ajax.start();
    }
};

/**
 * 查询订单信息列表
 */
UserInfo.search = function () {
    var queryData = {};
    queryData['userName'] = $("#userName").val();
    queryData['userPhone'] = $("#userPhone").val();
    queryData['address'] = $("#address").val();
    UserInfo.table.refresh({query: queryData});
};

UserInfo.import=function () {
    top.layer.open({
        type: 1,
        area: [500, 300],
        title:"导入数据",
        content:$("#importBox").html() ,
        btn: ['下载模板','确定', '关闭'],
        btn1: function(index, layero){
            window.location.href=Feng.ctxPath+'userInfo/import/template';
        },
        btn2: function(index, layero){
            var inputForm =top.$("#importForm");
            var form = new FormData(top.$("#importForm")[0]);
            console.log("form:"+form.get("name"));
            $.ajax({
                url:Feng.ctxPath+"/userInfo/import",
                type:"post",
                data:form,
                processData:false,
                contentType:false,
                success:function(data){
                    UserInfo.table.refresh();
                    Feng.success("导入成功!");
                },
                error:function(e){
                    Feng.error("导入失败!" + data.responseJSON.message + "!");
                }
            });

            top.layer.close(index);

        },

        btn3: function(index){
            top.layer.close(index);
        }
    });

}

// 显示加载框
UserInfo.loading=function loading(mess){
    if (mess == undefined || mess == ""){
        mess = "正在提交，请稍等...";
    }
    this.resetTip();
    top.$.jBox.tip(mess,'loading',{opacity:0});
}

UserInfo.loading=// 恢复提示框显示
    function resetTip(){
        top.$.jBox.tip.mess = null;
    }

$(function () {

    var defaultColunms = UserInfo.initColumn();
    var table = new BSTable(UserInfo.id, "/userInfo/list", defaultColunms);
    table.setPaginationType("server");
    UserInfo.table = table.init();

});
